<?php


# header of each page
function head($title='', $meta_description='', $meta_keywords='', $active='') {
header("Content-type: text/html; charset=UTF-8");
?>
<!DOCTYPE html>
<html>
<head>
<title><?=$title?></title>

<base href="<?=HTML_BASE?>" />

<meta name="description" content="<?=$meta_description?>" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">

<link rel="stylesheet" type="text/css" href="<?=HTML_BASE?>css/reset.css">
<link rel="stylesheet" type="text/css" href="<?=HTML_BASE?>css/style.css">
<script src="<?=HTML_BASE?>js/modernizr.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js" type="text/javascript"></script>



</head>

<body>
<div id="wrap"><div id="wrapin">
	<div id="top">
        <a id="logo" href="<?=HTML_BASE?>"></a>
        
		
		<div id="topabout">
			<a href="<?=HTML_BASE?>"><?=__HOME?></a> /
			<a href="<?=HTML_BASE.$_SESSION["lang"]?>/contacts"><?=__CONTACTS?></a>
		</div>
		
		<div id="topright">
			<? if ($_SESSION['USERNAME']) { ?>
				<?=__HELLO?> <a href="<?=HTML_BASE.$_SESSION["lang"]?>/login"><?=$_SESSION['USERNAME']?></a> / <a href="<?=HTML_BASE.$_SESSION["lang"]?>/user-setings"><?=__setings?></a> / <a href="<?=HTML_BASE?>logout.php"><?=__logout?></a>
			<? }else{ ?>
				<a href="<?=HTML_BASE.$_SESSION["lang"]?>/login"><?=__login?></a> / <a href="<?=HTML_BASE.$_SESSION["lang"]?>/register"><?=__register?></a>
			<? } ?>
			
            
            <br>
			<?php 
				$prm = '';
				if($_REQUEST["tmp"]) $prm .= '/'.$_REQUEST["tmp"];
				if($_REQUEST["nmr"]) $prm .= '/'.$_REQUEST["nmr"];
				
				
				
            	$sql = "SELECT lang_id,Name,code FROM language WHERE published  ORDER BY  	order_number ";
	
				$result_lang = query($sql);
				 while ($row_lang = mysql_fetch_object($result_lang)) {
					 $sel = '';
					 if($_SESSION["lang"] ==  $row_lang->code){
						  $sel = 'calss="active"';
					 }
			?>
            	<a href="<?=HTML_BASE?><?=$row_lang->code?><?=$prm?>" title="<?=$row_lang->Name?>" <?=$sel?> ><?=$row_lang->code?></a> /
            <?php }?>
            
            
        </div>
    </div>
    
   
    
  
<?php	
}

# footer of each page
function foot() {
	?>
 	<div id="footer">
    	&copy; <?=date('Y')?> <?=__COPYRIGHT?><br>
        <a href="http://studiokipo.com" target="_blank"><?=__KIPO?></a>
    </div>

<script src="<?=HTML_BASE?>js/script.js" type="text/javascript"></script>
</div></div>
</body>
</html>
<?php
}
# a redirecting function that adds the session ID if cookies are disabled and stops the execution of the script
function redirect($location) {
	if (!$location) error("Missing arguments.");

	$a = parse_url($location);
	if (!isset($a['query'])) {
		$a['query'] = '';
	}
	
	$session_delimiter = ($a['query'])?'&':'?';
	header("Location: $location".addsession($session_delimiter));
	exit;
}

# adding the session_id when cookies are disabled
function addsession($symbol="?") {
	return (!isset($_COOKIE[session_name()])&&session_id())?$symbol.session_name()."=".session_id():"";
}

function escape($string,$encoding=false) {
  return mysql_real_escape_string( (($encoding)?encoding($string):$string) );
}

function encoding($string) {
  return iconv('ISO-8859-1', 'UTF-8', $string);
}

function sth($string) {
  return htmlspecialchars(stripslashes($string));
}
# checks password
function checkPass($psw) {
	return preg_match("#.*^(?=.{6,20})(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9]).*$#", $pwd);
}
# checks whether an email is valid or not
function checkEmail($email) {
	return preg_match("/^[A-z0-9\.\-]+@(?:[A-z0-9\-]+\.)+[A-z0-9]{1,6}$/", $email);
}

# Cut a text to specified chracters length
function text_format($text, $length) {
	$str = '';
	$text_length = mb_strlen($text, "UTF-8");
	 
	if ($text_length < $length) {
		$str = $text;		
	} else {
		$position_end = strpos($text, ' ', $length);
		$str = substr($text, 0, ($position_end)?$position_end:$text_length);
	}
	
	return $str;
}

function text_format_blog($text, $length) {
	$str = '';
	$text_length = mb_strlen($text, "UTF-8");
	 
	if ($text_length < $length) {
		$str = $text;		
	} else {
		$position_end = strpos($text, '<hr />', $length);
		$str = substr($text, 0, ($position_end)?$position_end:$text_length);
	}
	
	return $str;
}


# general error function
function error($s="An error occured.") {
  if (!headers_sent()) {
    head("Error");
    echo "<table cellpadding=4 width=".SITE_WIDTH." align=center border=0><tr><td align=center class=title>$s</td></tr></table>";
    foot();
  }
  else
    echo $s;
  exit;
}

# debug function for dumping a variable value
function pp($var, $stop=false) {
  echo "<pre>";
  print_r($var);
  echo "</pre>";
  if ($stop) exit;
}

# Generate numbers
function generateNumber () {

  # start with a blank number
  $number = "";

  # define possible characters
  $possible = "0123456789"; 

  # pick a random character from the possible ones
  $number = substr($possible, mt_rand(0, strlen($possible)-1), 1);

  # done!
  return $number;

}


#Генератор на пароли

function generatePassword($length) {
	$vowels = 'aeuy';
	$consonants = 'BDGHJLMNPQRSTVWXZ23456789@#$%';
 
	$password = '';
	$alt = time() % 2;
	for ($i = 0; $i < $length; $i++) {
		if ($alt == 1) {
			$password .= $consonants[(rand() % strlen($consonants))];
			$alt = 0;
		} else {
			$password .= $vowels[(rand() % strlen($vowels))];
			$alt = 1;
		}
	}
	return $password;
}




function get_user_name($u_id){
	$sql = "SELECT
			full_name
		FROM
			users
		WHERE
			user_id = '$u_id'
			
		";
	$result = query($sql);
	$row = mysql_fetch_object($result);
	
	return $row->full_name;
}

function get_task_files($path)
{
	if(is_dir("../files/task-".$path))
	{
	$dir = opendir( '../files/task-'.$path );
	$p1 = explode('/',$path);
	$p2 = $p1[0];
	$f = '';
	while ( false != ( $filename = readdir($dir)))
		   { if (( $filename != "." ) and ( $filename != ".." ))
			  {  
			  $pth = base64_encode("task-".$path."/".$filename);
			  	$f .=  " <a href='fileopen.php?download_file=$pth&task=$p2' title='Download $filename' target='_blank'>$filename</a>,";
			  }
		   }
	closedir($dir);
	return substr($f,0,-1);
	}
}
# Menue of each user page
function menue(){
?>
 <div id="menu">
 		<a href="<?=HTML_BASE.$_SESSION["lang"]?>/focus"><?=__Focus?></a>
        <a href="<?=HTML_BASE.$_SESSION["lang"]?>/principle"><?=__Principle?></a>
        <a href="<?=HTML_BASE.$_SESSION["lang"]?>/practise"><?=__Practise?></a>
 </div>
  
     
<?
}
# Menue of each user page
function menu_user(){
?>
 	<div id="menu">
        <a href="<?=HTML_BASE.$_SESSION["lang"]?>/dashboard"><?=__dashboard?></a>
        <a href="<?=HTML_BASE.$_SESSION["lang"]?>/correspondences"><?=__correspondences?></a>
        <a href="<?=HTML_BASE.$_SESSION["lang"]?>/tasks"><?=__tasks?></a>
    </div>
  
     
<?
}


# Menue of each law page
function menu_law(){
?>


 	<div id="menu">
        <a href="<?=HTML_BASE.$_SESSION["lang"]?>/law"><?=__dashboard?></a>
        <a href="<?=HTML_BASE.$_SESSION["lang"]?>/correspondences"><?=__correspondences?></a>
        <a href="<?=HTML_BASE.$_SESSION["lang"]?>/tasks"><?=__tasks?></a>
        
      
        <form style="float:right; margin:3px 0 0;">
		<select class="inputbox" id="clients" >
		<option value='0'   >Всички клиенти</option>
		  <?php $sql_w = "SELECT
					u.user_id,
					u.full_name
				FROM
					users AS u,
					law_users AS l
					WHERE
					l.law_id = '". $_SESSION['USER_SID']."'
					AND
					l.use_id = u.user_id
					
				";
		$result_w = query($sql_w);
		 while($row_w = mysql_fetch_object($result_w))
		 {
			 $sel = '';
			 if($row_w->user_id == $_SESSION['client']) $sel = 'selected="selected"';
			 
		echo "<option value='".$row_w->user_id."' ".$sel." >".$row_w->full_name."</option>";
		
		}?>
		   
		</select>
	</form>
        <script>
		
		$( document ).ready(function() {
	
			$("#clients").change(function(){
				
				var client = $(this).attr('value');
				
				$.ajax({
					type: "POST",
					url: "change.php",
					data: "client=" + client,
					success: function(msg){
						
						location.reload();
					},
					error: function(msg){
						//alert(curr);
						location.reload();
					}
				});
			
			});
	
	});
	
		
</script>
        
    </div>
  
     
<?
}


?>